#include "ZL_JsonPrase.h"
// std-lib
#include <stdio.h>
#include <string.h>
// lib
#include "json_parse.h"

#define DEBUG_PRINTF(str, ...)   printf("[%s][%u]"str"", __func__, __LINE__, ##__VA_ARGS__)

char URL[100] = {0};                        //升级包链接
uint8_t ota_state_flag = 0;                 //升级标志位



//buf：/upgrade","{"id":611407370,"params":{"name":"full","version":"V1.1.1","url":"https://ziot-file.zoomlion.com/v2/file/token_download/a4c9217d60bf46da93da52385d95ca3c","size":51612,"sign":"DAF2B8B1078728F11D18F0D68188E01B","signMethod":"md5","fileName":"app_v1.0.5.bin"},"version":"1.0"}"
uint8_t analyze_set_firmware(char *buf)
{
    DEBUG_PRINTF("buf:[%s]\r\n", buf);

    // char *p = strstr(buf, "\",");
    // if (p == NULL) {
    //     return 1;
    // }
    // p = p+3;// 多去一个 "
    // strcpy(buf, p);
    // // DEBUG_PRINTF("buf:[%s]\r\n", buf);
    // //"{"id":611407370,"params":{"name":"full","version":"V1.1.1","url":"https://ziot-file.zoomlion.com/v2/file/token_download/a4c9217d60bf46da93da52385d95ca3c","size":51612,"sign":"DAF2B8B1078728F11D18F0D68188E01B","signMethod":"md5","fileName":"app_v1.0.5.bin"},"version":"1.0"}"
    // //去除前后"号
    // int len = strlen(buf);
    // buf[len-1] = 0;// 有 \r\n 应该要多
    // len = strlen(buf);
    // DEBUG_PRINTF("len:%d, buf:[%s]\r\n", len, buf);
    // //{"id":611407370,"params":{"name":"full","version":"V1.1.1","url":"https://ziot-file.zoomlion.com/v2/file/token_download/a4c9217d60bf46da93da52385d95ca3c","size":51612,"sign":"DAF2B8B1078728F11D18F0D68188E01B","signMethod":"md5","fileName":"app_v1.0.5.bin"},"version":"1.0"}

    // DEBUG_PRINTF("id=%d\r\n", JsonPraseInt(JsonPath(buf, "$.id")));
    char szTestBuffer[10] = {0};
    char *p = JsonPath(buf, "$.params.version");
    JsonPraseStr(p, szTestBuffer, sizeof(szTestBuffer));
    DEBUG_PRINTF("version:%s\r\n", szTestBuffer);

    // if ((p = JsonPath(buf, "$.params.url")) == NULL)
    // {
    //     DEBUG_PRINTF("JsonPath error\r\n");
    //     return 1;
    // }
    // DEBUG_PRINTF("p:%s\r\n",p);
    // memset(URL,0,sizeof(URL));
    // JsonPraseStr(p, URL, sizeof(URL) - 1);
    // DEBUG_PRINTF("URL:%s\r\n",URL);
    // ota_state_flag = 1;

    return 0;
}



/// @brief 中联JSON解析测试
void zhonglian_json_prase_test(void) {

    // char szTestBuffer[500] = "\"/ota/KT2yZiuN/5cebcd72/device/upgrade\",\"{\"id\":764651304,\"params\":{\"name\":\"full\",\"version\":\"2.0.6.0\",\"url\":\"https://ziot-file.zoomlion.com/v2/file/token_download/5efa49486b3c4b1b9583090d9bc69474\",\"size\":51140,\"sign\":\"8EABD61946D7E400CAE0F62C1D33EC5C\",\"signMethod\":\"md5\",\"fileName\":\"ZL_V2.0.6_2508061.bin\",\"extraInfo\":{}},\"version\":\"1.0\"}\"";

    // char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\"}}";

    // char szTestBuffer[500] = "{\"id\":764651304,\"params\":{\"name\":\"full\",\"version\":\"2.0.6.0\",\"url\":\"https://ziot-file.zoomlion.com/v2/file/token_download/5efa49486b3c4b1b9583090d9bc69474\",\"size\":51140,\"sign\":\"8EABD61946D7E400CAE0F62C1D33EC5C\",\"signMethod\":\"md5\",\"fileName\":\"ZL_V2.0.6_2508061.bin\",\"extraInfo\":{}},\"version\":\"1.0\"}";
    // char szTestBuffer[500] = "{\"id\":764651304,\"params\":{\"name\":\"full\",\"version\":\"2.0.6.0\",\"url\":\"https://ziot-file.zoomlion.com/v2/file/token_download/5efa49486b3c4b1b9583090d9bc69474\",\"size\":51140,\"sign\":\"8EABD61946D7E400CAE0F62C1D33EC5C\",\"signMethod\":\"md5\",\"fileName\":\"ZL_V2.0.6_2508061.bin\",\"extraInfo\":{}}}";
    // char szTestBuffer[500] = "{\"id\":764651304,\"params\":{\"name\":\"full\",\"version\":\"2.0.6.0\",\"url\":\"https://ziot-file.zoomlion.com/v2/file/token_download/5efa49486b3c4b1b9583090d9bc69474\",\"size\":51140,\"sign\":\"8EABD61946D7E400CAE0F62C1D33EC5C\",\"signMethod\":\"md5\",\"fileName\":\"ZL_V2.0.6_2508061.bin\"},\"version\":\"1.0\"}";

    // char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\"}}";
    // char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\"},\"extraInfo\":{}},\"version\":\"1.0\"}";

	// char szTestBuffer[500] = "{\"id\":764651304,\"params\":{\"name\":\"full\",\"version\":\"2.0.6.0\",\"url\":\"askfdjhgqwuyvkzudsvckabwervjzbcdiktuyeyqcgczghejcfatyewcfhgzjdcsifyvwqietfvagvdfuaghsvfjhvzhdbjfvaksjhgfvjhkg\",\"size\":51140,\"sign\":\"8EABD61946D7E400CAE0F62C1D33EC5C\",\"signMethod\":\"md5\",\"fileName\":\"ZL_V2.0.6_2508061.bin\",\"extraInfo\":{}},\"version\":\"1.0\"}";

	// char szTestBuffer[500] = "{\"params\":{\"extraInfo\":{},\"version\":\"2.0.6.0\"}}";				// 解析异常
	// char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\",\"extraInfo\":{}}}";				// 解析异常
	// char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\",\"extraInfo\":null}}";			// 解析正常
    // char szTestBuffer[500] = "{\"params\":{\"version\":\"2.0.6.0\"}}";								// 解析正常
	// char szTestBuffer[500] = "{\"params\":{\"extraInfo\":{},\"version\":\"2.0.6.0\"}}";				// 解析异常
	char szTestBuffer[500] = "{\"x\":{\"y\":{}}}";				                                    // 解析异常
	// char szTestBuffer[500] = "{\"y\":{}}";				                                            // 解析正常

    DEBUG_PRINTF("[%s]\n", szTestBuffer);

    char szVer[10] = {0};

	// while (1) {
		char *p = JsonPath(szTestBuffer, "$.x.y");
		// char *p = JsonPath(szTestBuffer, "$.params.version");
        json_type_t tType = JsonGetType(p);
		DEBUG_PRINTF("type:%d\r\n", tType);
	// }

    // analyze_set_firmware(szTestBuffer);

}
